import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/mine/my_purse/my_purse_controller.dart';
import 'package:flutter_changdian_football/pages/mine/my_purse/withdraw/withdraw_controller.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/number_input_limit.dart';
import 'package:flutter_changdian_football/widget/custom_button.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/sheet/param_picker.dart';
import 'package:get/get.dart';

// 提现
class WithdrawPage extends StatelessWidget {
  const WithdrawPage({
    super.key,
    this.type = CustomConstant.withdraw_normal,
    this.clubId
  });
  // 提现类型 finance-财政  normal-普通
  final String type;

  final String? clubId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF8FAF9),
      resizeToAvoidBottomInset: false,
      body: GetBuilder<MyPurseController>(
        builder: (myPurseController) {
          return GetBuilder<WithdrawController>(
            init: WithdrawController(clubId: clubId),
            builder: (withdrawController) {
              return Column(
                children: [
                  Stack(
                    children: [
                      Stack(
                        clipBehavior: Clip.none,
                        alignment: Alignment.center,
                        children: [
                          Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              SizedBox(
                                width: double.infinity,
                                height: 240.w,
                                child: Image.asset(
                                  'assets/images/img_me_bg.png',
                                  width: double.infinity,
                                  height: 240.w,
                                  fit: BoxFit.fill,
                                ),
                              ),

                              SizedBox(height: 72.w,)
                            ],
                          ),

                          Positioned(
                            bottom: 0.w,
                            child: SizedBox(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  SizedBox(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      children: [
                                        Container(
                                          padding: EdgeInsets.only(bottom: 4.w),
                                          child: Text('可提现金额(￥)', style: TextStyle(fontSize: 12.sp, color: Colors.white),),
                                        ),
                                        SizedBox(width: 10.w,),
                                        SizedBox(
                                          child: Text(myPurseController.userAccountBalance, style: TextStyle(fontSize: 26.sp, color: Colors.white, fontWeight: FontWeight.w500),),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 20.w,),
                                  Container(
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(16.r)
                                    ),
                                    width: MediaQuery.of(context).size.width - 34.w,
                                    padding: EdgeInsets.all(20.w),
                                    child: Column(
                                      mainAxisSize: MainAxisSize.min,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Container(
                                          alignment: Alignment.centerLeft,
                                          child: Text('提现金额', style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)),),
                                        ),
                                  
                                        SizedBox(height: 26.w,),
                                  
                                        Row(
                                          children: [
                                            SizedBox(
                                              child: Text('￥', style: TextStyle(fontSize: 32.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),),
                                            ),
                                            Expanded(
                                              child: TextField(
                                                style: TextStyle(fontSize: 18.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),
                                                controller: withdrawController.balanceController,
                                                keyboardType: TextInputType.number,
                                                inputFormatters: [
                                                  NumberInputLimit(
                                                    digit: 2,
                                                    max: 9999999
                                                  )
                                                ],
                                                decoration: InputDecoration(
                                                  isCollapsed: true,
                                                  hintText: '请输入金额',
                                                  hintStyle: TextStyle(fontSize: 18.sp, color: const Color(0xffcccccc), fontWeight: FontWeight.w500),
                                                  counterText: "",
                                                  contentPadding: EdgeInsets.symmetric(vertical: 0.w, horizontal: 10.w),
                                                  suffixIcon: GestureDetector(
                                                    behavior: HitTestBehavior.translucent,
                                                    onTap: () {
                                                      withdrawController.changeAllWithDraw(myPurseController.userAccountBalance);
                                                    },
                                                    child: Container(
                                                      padding: EdgeInsets.only(right: 11.w),
                                                      child: Text('全部提现', style: TextStyle(fontSize: 14.sp, color: const Color(0xff499360), fontWeight: FontWeight.w500),),
                                                    ),
                                                  ),
                                                  border: InputBorder.none,
                                                  suffixIconConstraints: BoxConstraints(
                                                    maxHeight: 32.w
                                                  )
                                                ),
                                              ),
                                            )
                                          ],
                                        ),
                                  
                                        SizedBox(height: 17.w,),
                                  
                                        Container(
                                          width: double.infinity,
                                          height: 1.w,
                                          color: const Color(0xffFAFAFA),
                                        ),
                                  
                                        SizedBox(height: 2.w,),
                                  
                                        SizedBox(
                                          child: Text('*最低提现金额必须大于1元', style: TextStyle(fontSize: 12.sp, color: const Color(0xffF4333C)),),
                                        )
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            )
                          )
                        ],
                      ),

                      Positioned(
                        top: 0,
                        left: 0,
                        right: 0,
                        child: PreferredSize(
                          preferredSize: const Size.fromHeight(kToolbarHeight), 
                          child: AppBar(
                            elevation: 0, 
                            backgroundColor: const Color(0xffF8FAF9).withOpacity(0),
                            iconTheme: const IconThemeData(
                              color: Colors.white
                            ),
                            title: Text('提现', style: TextStyle(fontSize: 16.sp, color: Colors.white),),
                            centerTitle: true,
                          ),
                        ),
                      )
                    ],
                  ),

                  SizedBox(height: 20.w,),

                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      withdrawController.getPayProperties();
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 17.w),
                      child: Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(16.r),
                          color: Colors.white
                        ),
                        padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 20.w),
                        child: Row(
                          children: [
                            SizedBox(
                              child: Text('提现到', style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)),),
                            ),
                  
                            const Spacer(),
                  
                            SizedBox(
                              child: Row(
                                children: [
                                  SizedBox(
                                    width: 22.w,
                                    height: 22.w,
                                    child: Image.asset('assets/images/img_zfb.png'),
                                  ),
                                  SizedBox(width: 6.w,),
                  
                                  SizedBox(
                                    child: Text(withdrawController.alipayAccount ?? '去绑定', style: TextStyle(fontSize: 15.sp, color: const Color(0xff333333)),),
                                  ),
                                  SizedBox(width: 8.w,),
                                  SizedBox(
                                    width: 10.w,
                                    height: 10.w,
                                    child: Image.asset('assets/images/icon_arrow_grey_right.png'),
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  ),

                  if (type == CustomConstant.withdraw_finance) ...[
                    SizedBox(height: 10.w,),

                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 17.w),
                      child: GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: () {
                          List<String> list = [
                            '场地费支取', '团队活动支出', '物品采购', '其他'
                          ];
                          int selecteds = -1;
                          for (int i = 0; i < list.length; i++) {
                            if (list[i] == withdrawController.reason) {
                              selecteds = i;
                            }
                          }
                          ParamPicker.show(
                            '提现理由', 
                            context, list,
                            selecteds: selecteds,
                            onConfirm: (v) {
                              withdrawController.reason = list[v];
                              withdrawController.update();
                            }
                          );
                        },
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(16.r),
                            color: Colors.white
                          ),
                          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 20.w),
                          child: Row(
                            children: [
                              SizedBox(
                                child: Text('提现理由', style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)),),
                              ),
                      
                              const Spacer(),
                      
                              SizedBox(
                                child: Row(
                                  children: [
                                    SizedBox(
                                      child: Text(withdrawController.reason ?? '请选择提现理由', style: TextStyle(fontSize: 15.sp, color: withdrawController.reason == null ? const Color(0xffcccccc) : const Color(0xff333333)),),
                                    ),
                                    SizedBox(width: 8.w,),
                                    SizedBox(
                                      width: 10.w,
                                      height: 10.w,
                                      child: Image.asset('assets/images/icon_arrow_grey_right.png'),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ),







                    SizedBox(height: 10.w,),

                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 17.w),
                      child: Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(16.r),
                          color: Colors.white
                        ),
                        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 20.w),
                        child: Row(
                          children: [
                            SizedBox(
                              child: Text('备注', style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)),),
                            ),

                            Expanded(
                              child: Container(
                                margin: EdgeInsets.only(left: 15.w),
                                child: Column(
                                  children: [
                                    TextField(
                                      keyboardType: TextInputType.text,
                                      maxLines: 1,
                                      textAlign: TextAlign.end,
                                      decoration: InputDecoration(
                                        contentPadding: EdgeInsets.symmetric(vertical: 0.w),
                                        border: const OutlineInputBorder(
                                          borderSide: BorderSide.none,
                                        ),
                                        hintStyle: TextStyle(fontSize: 15.sp, color: const Color(0xffCCCCCC), height: 1.5),
                                        hintText: '填写备注',
                                        counterText: '',
                                      ),
                                      style: TextStyle(fontSize: 15.sp, color: const Color(0xff333333), height: 1.5),
                                      onChanged: (v) {
                                      },
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],

                  const Spacer(),

                  Container(
                    padding: EdgeInsets.all(17.w),
                    child: CustomButton(
                      text: '立即提现',
                      onTap: () {
                        withdrawController.zfbWithdraw(type: type);
                      },
                    ),
                  )
                ],
              );
            }
          );
        }
      ),
    );
  }
}